package com.xkcoding.task.controller;

import com.alibaba.fastjson.JSON;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xkcoding.task.config.ArtemisPostTest;
import com.xkcoding.task.config.dto.PersonGroupOperation;
import com.xkcoding.task.dto.PersonInfoDto;
import com.xkcoding.task.repository.entity.PersonGroupEntity;
import com.xkcoding.task.repository.entity.StudentZsEntity;
import com.xkcoding.task.service.PersonGroupService;
import com.xkcoding.task.service.StudentZsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/personGroup")
@RequiredArgsConstructor
public class PersonGroupController {

  @Resource
  StudentZsService studentZsService;

  @Resource
  PersonGroupService personGroupService;

  @GetMapping("/syn")
  public List<PersonInfoDto.DataDTO.PersonListDTO> sybAll(String orgIndexCodes) {
    List<PersonInfoDto.DataDTO.PersonListDTO> resultList = new LinkedList<>();
    try {
      Integer pageNo = 1;
      Integer pageSize = 1000;
      Long totalPage = 0L;
      while (true) {
        String tempString = ArtemisPostTest.advancePersonList(orgIndexCodes, pageNo, pageSize);
        PersonInfoDto personInfoDto = JSON.parseObject(tempString, PersonInfoDto.class);
        if (null != personInfoDto && "0".equals(personInfoDto.getCode())) {
          Long totalRecord = personInfoDto.getData().getTotal();
          if (totalRecord % pageSize == 0) {
            totalPage = totalRecord / pageSize;
          } else {
            totalPage = totalRecord / pageSize + 1;
          }
          List<PersonInfoDto.DataDTO.PersonListDTO> list = personInfoDto.getData().getList();
          resultList.addAll(list);
          if (totalPage <= pageNo) {
            break;
          }
          pageNo++;
        }
      }
      log.info("获取全部人员数据成功");
      List<PersonGroupEntity> list = personGroupService.list();
      //key ----personGroupName
      //value ----- personGroupId
      Map<String, String> collect = list.stream().collect(Collectors.toMap(PersonGroupEntity::getPersonGroupName, PersonGroupEntity::getPersonGroupId));


      List<StudentZsEntity> studentZs = studentZsService.list();
      Map<String, StudentZsEntity> collect2 = studentZs.stream().collect(Collectors.toMap(StudentZsEntity::get学号, a -> a, (v1, v2) -> v1));
      List<String> jinZhiList = studentZs.stream().map(e -> e.get学号()).collect(Collectors.toList());
      Map<String, String> collect1 = resultList.stream().collect(Collectors.toMap(PersonInfoDto.DataDTO.PersonListDTO::getJobNo, PersonInfoDto.DataDTO.PersonListDTO::getPersonId));

      log.info("开始遍历");

      jinZhiList.forEach(a -> {
        String sushelouName = collect2.get(a).get宿舍楼名称();

        String personGroupId = collect.get(sushelouName);
        if (StringUtils.isNotBlank(personGroupId)) {
          PersonGroupOperation personGroupOperation = new PersonGroupOperation();
          personGroupOperation.setPersonGroupId(personGroupId);
          PersonGroupOperation.AddPersonInfosDTO addPersonInfosDTO = new PersonGroupOperation.AddPersonInfosDTO();
          String personId = collect1.get(a);
          if (StringUtils.isNotBlank(personId)) {
            addPersonInfosDTO.setPersonId(personId);
          }
          List<PersonGroupOperation.AddPersonInfosDTO> addPersonInfos = new LinkedList<>();
          addPersonInfos.add(addPersonInfosDTO);
          personGroupOperation.setAddPersonInfos(addPersonInfos);
          try {
            String s = ArtemisPostTest.acsPersonGroupOperation(personGroupOperation);
            log.info("success :{}",JSON.toJSONString(personGroupOperation));
          } catch (Exception e) {
            log.error("", e);
            throw new RuntimeException(e);
          }

        } else {
          log.info("this student jobNo:{} not in jingzhi", a);
        }
      });
      log.info("init success");
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
    return resultList;
  }

  @Resource
  StudentZsService authService;

  @GetMapping("/demo1")
  @ApiOperationSupport(order = 1)
  public List<StudentZsEntity> login() {
    return authService.list();
  }


}
